# _*_ coding: utf-8 _*_
from odoo import api, fields, models
from odoo.tools.translate import _
from odoo.exceptions import UserError
import odoo.addons.decimal_precision as dp


class SaleOrderLine(models.Model):
    _inherit = "sale.order.line"

    @api.multi
    @api.depends('price_unit', 'standard_price')
    def _compute_confirm_visible(self):
        if self.price_unit == self.standard_price:
            return True

    standard_price = fields.Float("Standard Price", digits=dp.get_precision('Product Price'))
    standard_price_invisible = fields.Float(u"标准价格",related='standard_price', digits=dp.get_precision('Product Price'))
                                  # compute="_compute_standard_price")

    reason = fields.Char("Reason")
    confirm_visible = fields.Boolean(compute=_compute_confirm_visible, default=False)

    @api.onchange('product_id', 'product_uom', 'product_uom_qty')
    def _onchange_standard_price(self):
        for line in self:
            if line.product_id and line.order_id.partner_id and line.order_id.pricelist_id:
                line.standard_price = line._get_display_price(line.product_id)

    @api.multi
    @api.onchange('product_id')
    def product_id_change(self):
        res = super(SaleOrderLine, self).product_id_change()
        # for line in self:
        #     line.update({
        #         "price_unit": 0
        #     })
        if "value" not in res:
            res["value"] = {
                "price_unit": 0
            }
        else:
            res["value"]["price_unit"] = 0
        return res

    @api.onchange('product_uom', 'product_uom_qty')
    def product_uom_change(self):
        res = super(SaleOrderLine, self).product_uom_change()
        if not res:
            res = {}
        if "value" not in res:
            res["value"] = {
                "price_unit": 0
            }
        else:
            res["value"]["price_unit"] = 0
        return res

    # @api.model
    # def create(self, vals):
    #     line = super(SaleOrderLine, self).create(vals)
    #     if not vals.get("reason"):
    #         standard_price = self.env['account.tax']._fix_tax_included_price(
    #             line._get_display_price(line.product_id),
    #             line.product_id.taxes_id, line.tax_id)
    #         if vals.get("price_unit") != standard_price:
    #             raise UserError(_("Unit Price is not equal to standard price,please input a reason !"))

    # @api.multi
    # def write(self, vals):
    #     reason = vals.get("reason") if "reason" in vals.keys() else self.reason
    #
    #     if not reason:
    #         price_unit = vals.get("price_unit") if "price_unit" in vals.keys() else self.price_unit
    #         standard_price = vals.get("standard_price") if "standard_price" in vals.keys() else self.standard_price
    #         if price_unit != standard_price:
    #             raise UserError(_("Unit Price is not equal to standard price,please input a reason !"))
    #
    #     return super(SaleOrderLine, self).write(vals)
